package compile

import (
	"strconv"
)

func _numToInteger(vstr string, case_ numType) (v interface{}, ok bool) {
	var err error

	switch case_ {

	case _INTEGER: // int32
		v, err = strconv.ParseInt(vstr, 10, 32)
		goto errorh

	case _LONG: // int64
		v, err = strconv.ParseInt(vstr, 10, 64)
		goto errorh

	case _FLOAT: // float32
		v, err = strconv.ParseFloat(vstr, 32)
		goto errorh

	case _DOUBLE: // float64
		v, err = strconv.ParseFloat(vstr, 64)
		goto errorh

	default:
		return 0, false
	}

errorh:
	if err != nil {
		return v, false
	}
	return v, true
}

func numberToInt64(vstr string) (v int64, ok bool) {
	tv, ok := _numToInteger(vstr, _LONG)
	if ok {
		v = tv.(int64)
	}
	return
}

func numberToInt32(vstr string) (v int, ok bool) {
	tv, ok := _numToInteger(vstr, _INTEGER)
	if ok {
		v = tv.(int)
	}
	return
}

func numberToFloat32(vstr string) (v float32, ok bool) {
	tv, ok := _numToInteger(vstr, _FLOAT)
	if ok {
		v = tv.(float32)
	}
	return
}

func numberToFloat64(vstr string) (v float64, ok bool) {
	tv, ok := _numToInteger(vstr, _DOUBLE)
	if ok {
		v = tv.(float64)
	}
	return
}
